package com.fengyi.homefurnish.service.impl;

import com.fengyi.homefurnish.controller.vo.StoreInfoVo;
import com.fengyi.homefurnish.mapper.SellAreaMapper;
import com.fengyi.homefurnish.mapper.StoreInfoMapper;
import com.fengyi.homefurnish.pojo.DtArea;
import com.fengyi.homefurnish.pojo.SellArea;
import com.fengyi.homefurnish.pojo.StoreInfo;
import com.fengyi.homefurnish.service.StoreInfoService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class StoreInfoServiceImpl implements StoreInfoService {

    @Resource
    private StoreInfoMapper storeInfoMapper;

    @Resource
    private SellAreaMapper sellAreaMapper;

    @Override
    public List<StoreInfoVo> search() {
        List<StoreInfo> storeList = storeInfoMapper.search();
        List<StoreInfoVo> stoVoList = new ArrayList<StoreInfoVo>();

        //循环把数据添加到VO对象
        StoreInfoVo stoVo = null;
        for(StoreInfo sto:storeList){

            //完整地区集合
            List<String> wanZhengArea = new ArrayList<String>();
            stoVo = new StoreInfoVo();
            stoVo.setStoreInfoId(sto.getStoreInfoId());
            stoVo.setStoreInfoName(sto.getStoreInfoName());
            stoVo.setSellAreaId(sto.getSellAreaId());
            stoVo.setStoreInfoDesc(sto.getStoreInfoDesc());
            stoVo.setStoreInfoState(sto.getStoreInfoState());

            //获取覆盖地区id
            String sellAreaId = sto.getSellAreaId();
            String[] areaIds = sellAreaId.split(",");

            //循环获取覆盖地区id所对应的地区名称
            for(int i=0; i<areaIds.length; i++){
                Integer areaId = Integer.parseInt(areaIds[i]);
                //单个地区名称集合
                List<String> areaName=new ArrayList<String>();
                boolean flag = true;
                //循环获取该地区所对应的地区名称和地区父编号
                do{
                    SellArea sellArea = sellAreaMapper.searchAreaById(areaId);
                    //添加单个地区到集合
                    areaName.add(sellArea.getDtArea().getAreaName());
                    //该地区所对应的父编号
                    Integer areaParentId = sellArea.getSellAreaPid();
                    if(areaParentId!=null && areaParentId!=0){
                        areaId=areaParentId;
                    } else {
                        flag=false;
                    }
                }while(flag==true);
                //把单个地区集合拼接成完整地区字符串
                StringBuffer sbf = new StringBuffer();
                int bei = 0;
                for(int j=areaName.size()-1; j>=0; j--){
                    sbf.append(areaName.get(j)+" ");
                    bei++;
                    if(bei==3){
                        //把完整地区添加到完整地区集合中
                        wanZhengArea.add(sbf.toString());
                        bei=0;
                        sbf = new StringBuffer();
                    }
                }
                //移除单个地区里的信息
                areaName.remove(areaName);
            }

            //添加vo对象中的地区集合
            stoVo.setAreaInfo(wanZhengArea);
            //添加到vo集合
            stoVoList.add(stoVo);
            //清空完整地区集合
            wanZhengArea.remove(wanZhengArea);
        }
        return stoVoList;
    }

    @Override
    public void saveStoreInfo(StoreInfo storeInfo) {
        storeInfoMapper.addStoreInfo(storeInfo);
    }

    @Override
    public void updateStoreInfo(StoreInfo storeInfo) {
        storeInfoMapper.updateStoreInfo(storeInfo);
    }

    @Override
    public void batchDelStoreInfo(String[] storeIds) {
        storeInfoMapper.batchDelSto(storeIds);
    }

    @Override
    public void updateStoreState(Integer stateId, Integer storeId) {
        Integer storeInfoState=null;
        if(stateId==0){
            storeInfoState=1;
        } else if(stateId==1){
            storeInfoState=0;
        }
        storeInfoMapper.updateStoreState(storeInfoState,storeId);
    }

    @Override
    public List<SellArea> getFirstArea() {
        return sellAreaMapper.getFirstArea();
    }

    @Override
    public List<SellArea> getAreaByPid(Integer pId) {
        return sellAreaMapper.getAreaByParentId(pId);
    }

    @Override
    public StoreInfoVo searchStoreInfoById(Integer storeInfoId) {
        StoreInfo sto = storeInfoMapper.searchStoreInfoById(storeInfoId);
        StoreInfoVo stoVo =  new StoreInfoVo();
        //完整地区集合
        List<String> wanZhengArea = new ArrayList<String>();
        stoVo = new StoreInfoVo();
        stoVo.setStoreInfoId(sto.getStoreInfoId());
        stoVo.setStoreInfoName(sto.getStoreInfoName());
        stoVo.setSellAreaId(sto.getSellAreaId());
        stoVo.setStoreInfoDesc(sto.getStoreInfoDesc());
        stoVo.setStoreInfoState(sto.getStoreInfoState());

        //获取覆盖地区id
        String sellAreaId = sto.getSellAreaId();
        String[] areaIds = sellAreaId.split(",");

        //循环获取覆盖地区id所对应的地区名称
        for(int i=0; i<areaIds.length; i++){
            Integer areaId = Integer.parseInt(areaIds[i]);
            //单个地区名称集合
            List<String> areaName=new ArrayList<String>();
            boolean flag = true;
            //循环获取该地区所对应的地区名称和地区父编号
            do{
                SellArea sellArea = sellAreaMapper.searchAreaById(areaId);
                //添加单个地区到集合
                areaName.add(sellArea.getDtArea().getAreaName());
                //该地区所对应的父编号
                Integer areaParentId = sellArea.getSellAreaPid();
                if(areaParentId!=null && areaParentId!=0){
                    areaId=areaParentId;
                } else {
                    flag=false;
                }
            }while(flag==true);
            //把单个地区集合拼接成完整地区字符串
            StringBuffer sbf = new StringBuffer();
            int bei = 0;
            for(int j=areaName.size()-1; j>=0; j--){
                sbf.append(areaName.get(j)+" ");
                bei++;
                if(bei==3){
                    //把完整地区添加到完整地区集合中
                    wanZhengArea.add(sbf.toString());
                    bei=0;
                    sbf = new StringBuffer();
                }
            }
            //移除单个地区里的信息
            areaName.remove(areaName);
        }

        //添加vo对象中的地区集合
        stoVo.setAreaInfo(wanZhengArea);
        //清空完整地区集合
        wanZhengArea.remove(wanZhengArea);
        return stoVo;
    }
}
